package com.meter.basic.controller;

import com.meter.basic.domain.BasicDevice;
import com.meter.basic.service.IBasicDeviceService;
import com.meter.common.annotation.Log;
import com.meter.common.core.controller.BaseController;
import com.meter.common.core.domain.AjaxResult;
import com.meter.common.core.page.TableDataInfo;
import com.meter.common.enums.BusinessType;
import com.meter.common.utils.ShiroUtils;
import com.meter.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping({"/basic/device"})
public class BasicDeviceController extends BaseController {

    private String prefix = "basic/device";

    @Autowired
    private IBasicDeviceService basicDeviceService;

    @RequiresPermissions({"basic:device:view"})
    @GetMapping
    public String device() {
        return prefix + "/device";
    }

    /**
     * 设备信息查询
     * @param basicDevice
     * @return
     */
    @RequiresPermissions({"basic:device:list"})
    @PostMapping({"/list"})
    @ResponseBody
    public TableDataInfo list(BasicDevice basicDevice) {
        startPage();
        List<BasicDevice> list = basicDeviceService.selectBasicDeviceList(basicDevice);
        return getDataTable(list);
    }

    @Log(title = "采集设备", businessType = BusinessType.IMPORT)
    @RequiresPermissions("basic:device:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasicDevice> util = new ExcelUtil<BasicDevice>(BasicDevice.class);
        List<BasicDevice> deviceList = util.importExcel(file.getInputStream());
        String message = basicDeviceService.importDevice(deviceList, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("basic:device:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BasicDevice> util = new ExcelUtil<BasicDevice>(BasicDevice.class);
        return util.importTemplateExcel("采集设备");
    }

    /**
     * 跳转到设备新增页面
     * @return
     */
    @GetMapping({"/add"})
    public String add() {
        return prefix + "/add";
    }

    /**
     * 设备新增
     * @param basicDevice
     * @return
     */
    @RequiresPermissions({"basic:device:add"})
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping({"/add"})
    @ResponseBody
    public AjaxResult addSave(BasicDevice basicDevice) {
        basicDevice.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(basicDeviceService.insertBasicDevice(basicDevice));
    }

    /**
     * 跳转到设备修改页面
     * @param deviceId
     * @param mmap
     * @return
     */
    @GetMapping({"/edit/{deviceId}"})
    public String edit(@PathVariable("deviceId") Long deviceId, ModelMap mmap) {
        BasicDevice basicDevice = basicDeviceService.selectBasicDeviceById(deviceId);
        mmap.put("basicDevice", basicDevice);
        return prefix + "/edit";
    }

    /**
     * 设备修改
     * @param basicDevice
     * @return
     */
    @RequiresPermissions({"basic:device:edit"})
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping({"/edit"})
    @ResponseBody
    public AjaxResult editSave(BasicDevice basicDevice) {
        basicDevice.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(basicDeviceService.updateBasicDevice(basicDevice));
    }

    /**
     * 设备删除
     * @param ids
     * @return
     */
    @RequiresPermissions({"basic:device:remove"})
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping({"/remove"})
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(basicDeviceService.deleteBasicDeviceByIds(ids));
    }

    /**
     * 校验imei号是否唯一
     */
    @PostMapping("/checkDeviceCodeUnique")
    @ResponseBody
    public boolean checkDeviceCodeUnique(BasicDevice basicDevice)
    {
        return basicDeviceService.checkDeviceCodeUnique(basicDevice);
    }
}
